package encrypt.demo;

import javax.crypto.Cipher;
import java.security.Key;
import java.util.Base64;
import java.util.Date;

public class AESDemo {
    public static void main(String[] args) throws Exception {
        Cipher RSA_cipher = Cipher.getInstance("AES");
        Cipher AES_cipher = Cipher.getInstance(AESUtils.TRANSFORMATION);
        Key key = AESUtils.generateKey("123456");
        Key key1 = AESUtils.generateKey("1234516");


        //测试AES加密
        long en_begin = new Date().getTime();
        AES_cipher.init(Cipher.ENCRYPT_MODE,key);
        byte[] encrypted_bytes = AES_cipher.doFinal("this is test data".getBytes());
        for (int i = 0; i < 100; i++) {
            AES_cipher.doFinal(("adfadsf"+i).getBytes());
        }
        String encoded_string = Base64.getEncoder().encodeToString(encrypted_bytes);
        long en_end = new Date().getTime();

        System.out.println(en_end-en_begin);
        System.out.println(encoded_string);

        //测试AES解密
        long de_begin = new Date().getTime();
        AES_cipher.init(Cipher.DECRYPT_MODE,key);
        encrypted_bytes = Base64.getDecoder().decode(encoded_string);
        byte[] decrypted_bytes = AES_cipher.doFinal(encrypted_bytes);
        String decryped_string = new String(decrypted_bytes);
        long de_end = new Date().getTime();


        System.out.println(de_end-de_begin);
        System.out.println(decryped_string);

    }
}
